package com.caisejingyu.whales.common.api.interceptor;

import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;


import com.caisejingyu.whales.common.AppConfig;
import com.caisejingyu.whales.common.L;
import com.caisejingyu.whales.common.api.ApiConfig;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * 普通网络请求拦截器（GET、DELETE、PUT、POST）
 *
 * @author Classic
 * @version v1.0, 2017/8/21 下午4:16
 */
@SuppressWarnings("unused")
public class BaseInterceptor extends AbsInterceptor {

    // 日志 log 输出信息
    private static final String TAG = BaseInterceptor.class.getSimpleName();
    
    public BaseInterceptor(@NonNull ApiConfig apiConfig) {
        super(apiConfig);
    }
    
    protected Response processResponse(@NonNull Response response) {
        if ((response.isSuccessful() || response.code() == 500) && response.body() != null) {
            String result = "{}";
            try {
                result = response.body().string();
            } catch (IOException ignore) { }
            Log.i(TAG, "net success " + "\n" + "-----> content = " + result + " \nurl = " + response.request().url());
            return responseBuilder.code(200)
                    .request(response.request())
                    .protocol(response.protocol())
                    .message("OK")
                    .body(ResponseBody.create(response.body().contentType(), result)).build();
        } else {
            return response;
        }
    }
    
}